package com.ts.opengl.simpleDraw

import android.R.attr
import android.content.Context
import android.graphics.BitmapFactory
import android.opengl.GLES20.GL_FLOAT
import android.opengl.GLES20.glEnableVertexAttribArray
import android.opengl.GLES20.glVertexAttribPointer
import android.opengl.GLES30
import android.opengl.GLUtils
import android.opengl.Matrix
import android.util.Log
import com.ts.audiocamera.R
import com.ts.opengl.IInterface.TransformRender
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10


class TexttureRender(var conext:Context): TransformRender() {

    private val POINT_RECT_DATA2 = floatArrayOf(
        // positions         //color              // texture coords
        -1.0f,  1.0f, 0.0f,   1.0f, 0.0f, 0.0f,   0.0f, 0.0f, // top right
        -1.0f, -1.0f, 0.0f,   1.0f, 0.0f, 1.0f,   0.0f, 1.0f, // bottom right
        1.0f, 1.0f, 0.0f,   1.0f, 0.0f, 1.0f,   1.0f,0.0f, // bottom left
        1.0f,  -1.0f, 0.0f,   0.0f, 0.5f, 1.0f,   1.0f, 1.0f  // top left
    )
    private val indeices = intArrayOf(
        // 注意索引从0开始!
        // 此例的索引(0,1,2,3)就是顶点数组vertices的下标，
        // 这样可以由下标代表顶点组合成矩形

        0, 1, 2, // 第一个三角形
        1, 2, 3  // 第二个三角形
    )
    var id=0;
    var vao=IntArray(1)
    var vbo=IntArray(1)
    var ebo=IntArray(1)



    private  val TAG = "TexttureRender"


    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES30.glClearColor(1f, 1f, 1f, 1f)
        program=openglUtil.createProgram(conext, R.raw.texture_ver,R.raw.texture_fra)



        val element=openglUtil.createFloatBuffer(POINT_RECT_DATA2)
        val indexData=openglUtil.createIntBuffer(indeices)


        //使用 vbo,vao 优化数据传递
        //创建 VAO
        GLES30.glGenVertexArrays(1, vao, 0)
        // //创建 VBO
        GLES30.glGenBuffers(1, vbo, 0)
        //绑定 VAO ,之后再绑定 VBO
        GLES30.glBindVertexArray(vao[0])
        //绑定VBO
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vbo[0])
        GLES30.glBufferData(
            GLES30.GL_ARRAY_BUFFER,
            element.capacity() * 4,
            element,
            GLES30.GL_STATIC_DRAW
        )
        //创建 ebo
        GLES30.glGenBuffers(1, ebo, 0)
        //绑定 ebo 到上下文
        GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, ebo[0])
        //昂丁
        GLES30.glBufferData(
            GLES30.GL_ELEMENT_ARRAY_BUFFER,
            indexData.capacity() * 4,
            indexData,
            GLES30.GL_STATIC_DRAW
        )


        //位置信息解析
        element.position(0)
        glVertexAttribPointer(0,3, GL_FLOAT,false,8*4,0)
        glEnableVertexAttribArray(0)

        element.position(3)
        glVertexAttribPointer(1,3, GL_FLOAT,false,8*4,3*4)
        GLES30.glEnableVertexAttribArray(1)

        id=loadText(R.drawable.mao)
        element.position(6)
        GLES30.glVertexAttribPointer(
            2, 2, GLES30.GL_FLOAT,
            false, 8 * 4, 6*4 //需要指定颜色的地址 3 * 4
        )
        GLES30.glEnableVertexAttribArray(2)


        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0)
        GLES30.glBindVertexArray(0)
        //注意顺序，ebo 要在 eao 之后
        GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, 0)
    }


    fun loadText(resid:Int) :Int{
        val textures=IntArray(1)
        //创建文理
        GLES30.glGenTextures(1,textures,0)
        //绑定文理
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,textures[0])

        BitmapFactory.Options().apply {
            inScaled=false
            val bitmap=BitmapFactory.decodeResource(conext.resources,resid,this)
            if (bitmap==null){
                GLES30.glDeleteTextures(1,textures,0)
                return@apply
            }

            GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,
                GLES30.GL_TEXTURE_MIN_FILTER,GLES30.GL_NEAREST)
            GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,
                GLES30.GL_TEXTURE_MAG_FILTER,GLES30.GL_LINEAR)

            GLUtils.texImage2D(GLES30.GL_TEXTURE_2D,0,bitmap,0)
//            GLUtils.te
//            GLES30.glGenerateMipmap(GLES30.GL_TEXTURE_2D)

            GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,0)
        }

        return textures[0];

    }


    var mWidth=0;
    var mHeight=0;
    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES30.glViewport(0, 0, width, height)
        mWidth=width;
        mHeight=height;



    }



    var mvpMatrix= FloatArray(16){1.0f}






    override fun onDrawFrame(gl: GL10?) {
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT)


        //useVaoVboAndEbo
        val ratio = attr.width.toFloat() / attr.height
        GLES30.glUseProgram(program)
        UpdateMVPMatrix(m_AngleX.toInt(),m_AngleY.toInt(),ratio)
        val loc=GLES30.glGetUniformLocation(program,"u_MVPMatrix")
        Log.e(TAG, "$loc---u_MVPMatrix")
        GLES30.glUniformMatrix4fv(loc,1,false,mMVPMatrix,0)


        GLES30.glBindVertexArray(vao[0])
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,id)
        GLES30.glDrawElements(GLES30.GL_TRIANGLE_STRIP, 6, GLES30.GL_UNSIGNED_INT, 0)
    }
}